package com.xsm.ppsecret.ui.pwd;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xsm.ppsecret.R;
import com.xsm.ppsecret.adapter.BaseRecyclerAdapter;
import com.xsm.ppsecret.adapter.PwdAdapter;
import com.xsm.ppsecret.model.PwdEntry;
import com.xsm.ppsecret.presenter.PwdPresenter;
import com.xsm.ppsecret.ui.BaseActivity;
import com.xsm.ppsecret.ui.implview.IPwdActivity;
import com.xsm.ppsecret.widgets.DeleteDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PwdActivity extends BaseActivity implements IPwdActivity, BaseRecyclerAdapter.OnChildClickListener, BaseRecyclerAdapter.OnLongChildClickListener, View.OnClickListener, DeleteDialog.DeleteListener {

    public static final int ADD_PWD = 521;
    @BindView(R.id.menu_normal_title)
    TextView menuTitle;
    @BindView(R.id.menu_normal_action)
    TextView menuAction;
    @BindView(R.id.menu_normal_back)
    ImageView menuBack;
    @BindView(R.id.pwd_recycler)
    RecyclerView mRecyclerView;
    private List<PwdEntry> mList;
    private PwdAdapter mAdapter;
    private PwdPresenter mPrestener = new PwdPresenter(this);
    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd);
        ButterKnife.bind(this);
        initView();
        loadData();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPrestener.dispose();
    }

    private void initView() {
        menuTitle.setText("密码本");
        menuAction.setText("添加");
        menuBack.setOnClickListener(this);
        menuAction.setOnClickListener(this);

        mList = new ArrayList<>();
        mAdapter = new PwdAdapter(this, mList);
        mAdapter.setOnChildClickListener(this);
        mAdapter.setOnLongChildClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        //设置增加 移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_normal_back:
                finish();
                break;
            case R.id.menu_normal_action:
                startActivityForResult(new Intent(this, AddPwdActivity.class), ADD_PWD);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == ADD_PWD) {
            loadData();
        }
    }

    @Override
    public void onChildClick(RecyclerView parent, View view, int position) {
        PwdEntry pwdWrapper = mList.get(position);
        Gson gson = new Gson();
        String json = gson.toJson(pwdWrapper);
        Intent intent = new Intent(this, PwdDetailActivity.class);
        intent.putExtra("json", json);
        startActivity(intent);
    }

    @Override
    public void onLongChildClick(RecyclerView parent, View view, int position) {
        mPosition = position;
        DeleteDialog dialog = new DeleteDialog(this, R.style.customDialogStyle, this);
        dialog.show();

    }

    @Override
    public void confirm() {
        PwdEntry pwdEntry = mList.get(mPosition);
        mPrestener.delete(this, pwdEntry);

    }

    public void loadData() {
        mPrestener.loadData(this);
    }

    @Override
    public void loadSuccessed(List<PwdEntry> data) {
        mList.clear();
        mList.addAll(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadError(String errorMsg) {
        Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void deleteSuccess(int value) {
        if (mPosition == 0) {
            if (mList.size() == 1) {
                mList.clear();
                mAdapter.notifyDataSetChanged();
            } else {
                loadData();
            }
        } else {
            loadData();
        }
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void deleteError() {
        Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
    }

}
